<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="text" id="inp">
		<script>
		var inp = document.getElementById("inp");
	/* 	var ind = null;
		inp.oninput =function(e){
			if(ind){
				clearTimeout(ind);
			}
			ind =setTimeout(()=>{
				console.log(inp.value,"发起ajax请求");
			},300)
		} */
		// 发起请求太频繁，如果用户停止输入300毫秒，再开始发起请求
		
		
		function debounce(fun,delay=500){
			let  timer = null; //定时器
			return function(){
				if(timer){clearTimeout(timer)} //如果已经有定时器就清除定时器
				timer = setTimeout(()=>{					
					fun.apply(this,arguments) //函数的参数
					timer = null;
				},delay)
			}
		}
		inp.addEventListener("input",debounce((e)=>{
			console.log(inp.value,"发起ajax请求",e);
		},500))
		</script>
	</body>
</html>
